﻿Imports System.Net.Mail
Imports System.IO
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Windows.Forms
Imports System.Reflection


Public Class Register
    Inherits System.Web.UI.Page

    Public dbmanager As DBManager = New DBManager()
    Private checkEmail As Boolean
    Private checkDisplay As Boolean
    Private Shared userID As Integer
    Private Shared imageID As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

    Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegister.Click
        Dim gender As Char
        Dim userID As String
        If rbtnFemale.Checked Then
            gender = "F"
        Else
            gender = "M"
        End If

        Dim KeyGen As RandomKeyGenerator
        Dim verificationKey As String

        KeyGen = New RandomKeyGenerator
        KeyGen.KeyLetters = "abcdefghijklmnopqrstuvwxyz"
        KeyGen.KeyNumbers = "0123456789"
        KeyGen.KeyChars = 12
        verificationKey = KeyGen.Generate()

        lblAvailName.Visible = False
        lblWarningName.Visible = False
        lblWarningEmail.Visible = False
        lblAvailEmail.Visible = False

        If (tbxEmail.Text.Trim.Length > 5) Then
            checkEmail = dbmanager.checkAvailEmail(tbxEmail.Text)
            If checkEmail = False Then
                lblWarningEmail.Visible = True
                lblWarningEmail.Text = "Email Address is not available"
                lblAvailEmail.Visible = False
            Else
                If (tbxDisplayName.Text.Length > 3) Then
                    checkDisplay = dbmanager.checkAvailDisplayName(tbxDisplayName.Text)
                    If checkDisplay = False Then
                        lblWarningName.Visible = True
                        lblWarningName.Text = "Display Name is not available"
                        lblAvailName.Visible = False
                    Else
                        If Session("randomStr").ToString() = tbxCaptcha.Text Then

                            lblWarningEmail.Text = False
                            Dim hash = dbmanager.hashPassword(tbxPassword.Text)
                            userID = dbmanager.addUser(tbxEmail.Text, hash, tbxDisplayName.Text, gender)
                            dbmanager.emailVerification(verificationKey, userID)
                            sendEmail(verificationKey)
                            MsgBox("Thank You for choosing GOODANOT! A verification link is sent to your email, please verify it. Thank You!")
                            tbxDisplayName.Text = ""
                            tbxEmail.Text = ""
                            tbxPassword.Text = ""
                            tbxRetypePassword.Text = ""
                            Response.Redirect("Home.aspx")
                        Else
                            MsgBox("You have entered an incorrect code")
                        End If
                    End If
                Else
                    lblWarningName.Visible = True
                    lblWarningName.Text = "Display Name must be 4 or more charcters"
                End If
            End If
        Else
            lblWarningEmail.Visible = True
            lblWarningEmail.Text = "Please enter in the following format: jenny@yahoo.com"
        End If
    End Sub

    Protected Sub sendEmail(ByVal verificationKey As String)
        Try

            Dim s As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
            Dim nc As System.Net.NetworkCredential = New System.Net.NetworkCredential("goodanot@gmail.com", "threepotatoes")
            s.EnableSsl = True
            s.UseDefaultCredentials = False
            s.Credentials = nc

            s.Send("goodanot@gmail.com", tbxEmail.Text, "GoodAnot Verification", "Please click on the link : " + "http://localhost:57166/Verify.aspx?code=" + verificationKey + " to verify your email")
        Catch ex As Exception
        End Try
    End Sub

    Protected Sub btnCheck2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck2.Click
        lblAvailName.Visible = False
        lblWarningName.Visible = False
        lblWarningEmail.Visible = False
        lblAvailEmail.Visible = False

        If (tbxDisplayName.Text.Length > 3) Then
            checkDisplay = dbmanager.checkAvailDisplayName(tbxDisplayName.Text)
            If checkDisplay = False Then
                lblWarningName.Visible = True
                lblWarningName.Text = "Display Name is not available"
                lblAvailName.Visible = False
                tbxDisplayName.ReadOnly = False
            Else
                lblAvailName.Visible = True
                lblAvailName.Text = "Display Name is available"
                lblWarningName.Visible = False
                tbxDisplayName.ReadOnly = True
            End If
        Else
            lblWarningName.Visible = True
            lblWarningName.Text = "Display Name must be 4 or more charcters"
        End If
    End Sub

    Protected Sub rbtnFemale_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbtnFemale.CheckedChanged

    End Sub

    Protected Sub cbxTC_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbxTC.CheckedChanged
        If (cbxTC.Checked) Then
            btnRegister.Enabled = True
        Else
            btnRegister.Enabled = False
        End If

    End Sub

    Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
        lblAvailName.Visible = False
        lblWarningName.Visible = False
        lblWarningEmail.Visible = False
        lblAvailEmail.Visible = False
        If (tbxEmail.Text.Trim.Length > 5) Then
            checkEmail = dbmanager.checkAvailEmail(tbxEmail.Text)
            If checkEmail = False Then
                lblWarningEmail.Visible = True
                lblWarningEmail.Text = "Email Address is not available"
                lblAvailEmail.Visible = False
                tbxEmail.ReadOnly = False
            Else
                lblAvailEmail.Visible = True
                lblAvailEmail.Text = "Email Address is available"
                lblWarningEmail.Visible = False
                tbxEmail.ReadOnly = True
            End If
        Else
            lblWarningEmail.Visible = True
            lblWarningEmail.Text = "Please enter in the following format: jenny@yahoo.com"
        End If
    End Sub

    Protected Sub tbxDisplayName_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbxDisplayName.TextChanged
        If (lblAvailName.Visible) Then
            lblAvailName.Visible = False
        End If
    End Sub

    Protected Sub tbxEmail_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbxEmail.TextChanged
        If lblAvailEmail.Visible Then
            lblAvailEmail.Visible = False
        End If
    End Sub


End Class